package com.sys.db.action;

import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;
import com.sys.db.manager.DefFlowManager;
import com.sys.db.manager.LogFlowManager;
import com.sys.db.manager.RunParaManager;
import com.sys.db.util.ThreadArray;
import com.sys.dispatch.FlowThread;
import com.sys.dispatch.HandFlowThread;
import com.sys.pojo.DefFlow;
import com.sys.pojo.RunPara;

public class RunParaAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List list=null;
	private RunParaManager runParaManager;
	private DefFlowManager defFlowManager;
	private LogFlowManager logFlowManager;
	private RunPara runPara;
	private String datas;
	private String ids;
	private int count;
	private String start;
	private String limit;
	private String flowId;
	private String dataDate;//手工运行的数据日期
	private String message;
	private boolean success=true;
	/**
	 * 列出所有jobType
	 * @return
	 */
	public String listRunParas(){
		try {
			List listAll=runParaManager.findAll();
			if(start!=null&&limit!=null){
				list=runParaManager.findByPage(new Integer(start), new Integer(limit));
				count=list.size();
			}else{
				list=listAll;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 保存页面修改/新增数据
	 */
	public String saveRunParas(){
		try {
			runParaManager.saveOrUpdate(datas);
		} catch (Exception e) {
			message=e.getMessage();
			success=false;
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	/**
	 * 删除选择的数据
	 */
	public String deleteRunParas(){
		try {
			runParaManager.deleteByIds(ids);
		} catch (Exception e) {
			message=e.getMessage();
			success=false;
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	/**
	 * 手工运行作业流
	 * @return
	 */
	public String  handRunFlow(){
		if(flowId!=null&&!"".equals(flowId)){
			boolean stopFlag=false;//作业流运行标志
			try {
				DefFlow df=defFlowManager.findById(new Integer(flowId));//要手工跑的作业流
				String dependIds=df.getDependId();//依赖的作业流
				dataDate=dataDate.replace("-", "");
				if(dependIds!=null&&!"".equals(dependIds)){
					String [] ids=dependIds.split(",");
					for(int i=0;i<ids.length;i++){
						List Loglist= logFlowManager.findByIdDateSuccess(new Integer(flowId), dataDate);
						if(Loglist==null||Loglist.size()==0){//根据跑的数据日期判断所依赖的作业流该天是数据是否跑成功
							stopFlag=true;
							message="依赖的作业流["+dataDate+"]的数据没有完成！";
							break;
						}
					}
				}
				if(!stopFlag){
					df.setStatus("1");
					defFlowManager.update(df);//更新作业流状态
					HandFlowThread hf=new HandFlowThread(df,dataDate);
					ThreadArray.addThread(df.getFlowId(), hf);
					hf.start();
					success=true;
				}
			}catch (Exception e) {
				e.printStackTrace();
				success=false;
			}
		}else{
			success=false;
			message="作业流ID为空！";
		}
		return SUCCESS;
	}
	public String stopThread(){
		if(flowId!=null&&!"".equals(flowId)){
			HandFlowThread tt= (HandFlowThread) ThreadArray.getThread(new Integer(flowId));
			if(tt!=null){
				while(tt.isAlive()){
					tt.interrupt();
				}
//				ThreadArray.moveThread(new Integer(flowId));
			}
		}else{
			success=false;
			message="作业流ID为空！";
		}
		return SUCCESS;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	@JSON(serialize=false)
	public RunParaManager getRunParaManager() {
		return runParaManager;
	}
	public void setRunParaManager(RunParaManager RunParaManager) {
		this.runParaManager = RunParaManager;
	}
	public RunPara getRunPara() {
		return runPara;
	}

	public void setRunPara(RunPara runPara) {
		this.runPara = runPara;
	}

	public String getDatas() {
		return datas;
	}

	public void setDatas(String datas) {
		this.datas = datas;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public String getStart() {
		return start;
	}
	public void setStart(String start) {
		this.start = start;
	}
	public String getLimit() {
		return limit;
	}
	public void setLimit(String limit) {
		this.limit = limit;
	}
	public String getFlowId() {
		return flowId;
	}
	public void setFlowId(String flowId) {
		this.flowId = flowId;
	}
	@JSON(serialize=false)
	public DefFlowManager getDefFlowManager() {
		return defFlowManager;
	}
	public void setDefFlowManager(DefFlowManager defFlowManager) {
		this.defFlowManager = defFlowManager;
	}
	public String getDataDate() {
		return dataDate;
	}
	public void setDataDate(String dataDate) {
		this.dataDate = dataDate;
	}
	@JSON(serialize=false)
	public LogFlowManager getLogFlowManager() {
		return logFlowManager;
	}
	public void setLogFlowManager(LogFlowManager logFlowManager) {
		this.logFlowManager = logFlowManager;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	
	
}
